package com.datas.controller.api;

import com.datas.beans.SearchCompanyParam;
import com.datas.service.api.ApiRegionService;
import com.datas.service.api.ApiUserService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by jige0727 on 2017/5/30.
 */
@Controller
public class ApiUserController {
    @Autowired
    ApiUserService apiUserService;
    @Autowired
    ApiRegionService apiRegionService;

    //TODO 校验 appkey
    static String appkey = "0cb11943b1e14163a0948acb15245e33";

    @ResponseBody
    @RequestMapping(value = "/userinfo", produces = "application/json;charset=UTF-8")
    public String userinfo(String access_token) {
        Map<String, Object> userType = apiUserService.getUserTypeBuToken(access_token);
        JSONObject res = new JSONObject();
        if (userType.get("userType").equals("ums")) {
            res = apiUserService.userinfo(access_token);
        } else {
            res = apiUserService.userinfoExpert(Integer.parseInt("" + userType.get("users_id")));
        }
        return res.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/region_all", produces = "application/json;charset=UTF-8")
    public String region_all() {
        JSONObject res = apiRegionService.region_all();
        return res.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/region_country", produces = "application/json;charset=UTF-8")
    public String region_country() {
        JSONObject res = apiRegionService.region_country();
        return res.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/search/company-all", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String searchCompany(SearchCompanyParam param) {
        JSONArray res = apiRegionService.searchCompany(param);
        return res.toString();
    }
}
